package global

import (
	"fmt"
	"github.com/go-gin-admin/util"
	"github.com/minio/minio-go/v6"
	"github.com/spf13/viper"
	"log"
)

// 初始化minio
var (
	MinioClient *minio.Client
	MyMinio     AppMinio
)

func InitMinio() {
	// 连接minio
	var err error
	MyMinio.MinioHost = viper.GetString("minio.minioHost")
	MyMinio.MinioPort = viper.GetInt64("minio.minioPort")
	MyMinio.MinioAccessKeyID = viper.GetString("minio.minioAccessKeyID")
	MyMinio.MinioSecretAccessKey = viper.GetString("minio.minioSecretAccessKey")
	MyMinio.MinioUseSSL = viper.GetBool("minio.minioUseSSL")
	MyMinio.BucketName = viper.GetString("minio.minioBucketName")
	MyMinio.MinioLocation = viper.GetString("minio.minioLocation")

	MinioClient, err = minio.New(MyMinio.MinioHost + ":" + util.IntToString(MyMinio.MinioPort), MyMinio.MinioAccessKeyID, MyMinio.MinioSecretAccessKey, MyMinio.MinioUseSSL)
	if err != nil {
		fmt.Sprintf("minio初始化失败, %s\n", err.Error())
	}
	fmt.Println("初始化成功", MinioClient)

	err = MinioClient.MakeBucket(MyMinio.BucketName, MyMinio.MinioLocation)
	if err != nil {
		// 检查储存桶是否存在
		exists, err := MinioClient.BucketExists(MyMinio.BucketName)
		if err == nil && exists {
			log.Println("已经存在当前桶\n")
		} else {
			log.Println("创建失败%s\n", err.Error())
		}
	}

}
